/* make thousand separate */
/*
 *  using by function formatCurrency!!
 */
function removeCharacter(v, ch) {
	var tempValue = v + "";
	var becontinue = true;
	while (becontinue == true) {
		var point = tempValue.indexOf(ch);
		if (point >= 0) {
			var myLen = tempValue.length;
			tempValue = tempValue.substr(0, point)
					+ tempValue.substr(point + 1, myLen);
			becontinue = true;
		} else {
			becontinue = false;
		}
	}
	return tempValue;
}

/*
 * using by function formatCurrency!!
 */
function characterControl(value) {
	var tempValue = "";
	var len = value.length;
	for (i = 0; i < len; i++) {
		var chr = value.substr(i, 1);
		if ((chr < '0' || chr > '9') && chr != '.' && chr != ',') {
			chr = '';
		}

		tempValue = tempValue + chr;
	}
	return tempValue;
}

/*
 * Automaticly converts the value in the textbox in a currency format with
 * thousands seperator and decimal point
 * 
 * @param value : the input text @param digit : decimal number after comma
 */
function formatCurrency(value, digit) {
	var thausandSepCh = ",";
	var decimalSepCh = ".";

	var tempValue = "";
	var realValue = value + "";
	var devValue = "";
	realValue = characterControl(realValue);
	var comma = realValue.indexOf(decimalSepCh);
	if (comma != -1) {
		tempValue = realValue.substr(0, comma);
		devValue = realValue.substr(comma);
		devValue = removeCharacter(devValue, thausandSepCh);
		devValue = removeCharacter(devValue, decimalSepCh);
		devValue = decimalSepCh + devValue;
		if (devValue.length > 3) {
			devValue = devValue.substr(0, 3);
		}
	} else {
		tempValue = realValue;
	}

	tempValue = removeCharacter(tempValue, thausandSepCh);

	var result = "";
	var len = tempValue.length;
	while (len > 3) {
		result = thausandSepCh + tempValue.substr(len - 3, 3) + result;
		len -= 3;
	}
	result = tempValue.substr(0, len) + result;
	
	if (value.indexOf("-") == 0)
		result = "-" + result;
	return result + devValue;
}

function validateCurrency(inputName) {
    var money;
    
    $('input[name^=' + inputName + ']').each(function() {
        money = $(this).val(); 
        money = money.replace(/[,.]/g, "");
        $(this).val(money);
    });
}

function curStartup(updateId) {
	object = $(updateId);
	object.value = formatCurrency(object.value, 2);
}